import 'package:safetymonitor/app/components/custom_select_function.dart';
import 'package:safetymonitor/app/components/toast_widget.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/http/http.dart';
import 'package:safetymonitor/app/core/utils/interface.dart';
import 'package:safetymonitor/app/data/entities/application/day_info_list_entity.dart';
import 'package:safetymonitor/app/data/entities/home/duty/change_shift_info_create_entity.dart';
import 'package:safetymonitor/app/data/entities/home/duty/major_event_info_create_entity.dart';
import 'package:safetymonitor/modules/home/home_page/duty_manager/duty/controller.dart';

///开始交接班
class StartTheShiftController extends GetxController {
  ///创建交接班信息实体类
  ChangeShiftInfoCreateEntity changeShiftInfoCreateEntity = ChangeShiftInfoCreateEntity();
  ///重大事件信息实体类
  MajorEventInfoCreateEntity majorEventInfoCreateEntity = MajorEventInfoCreateEntity();

  ///值班信息
  DayInfoListPlanInfo duty = DayInfoListPlanInfo();

  @override
  void onInit() {
    super.onInit();
    duty = Get.arguments as DayInfoListPlanInfo;
    //交班人员
    changeShiftInfoCreateEntity.handoverPerson = duty.dutyPerson;
    //交班人员电话
    changeShiftInfoCreateEntity.handoverPersonPhone = duty.dutyPersonPhone;
    //所属部门
    changeShiftInfoCreateEntity.handoverPersonDept = duty.companyName;
    //所属公司
    // changeShiftInfoCreateEntity.handoverPersonUnit = duty.companyName;
    //交班班次名字
    changeShiftInfoCreateEntity.handoverScheduleName = duty.scheduleName;
    //交班班次id
    changeShiftInfoCreateEntity.handoverScheduleConfigId = duty.scheduleConfigId;
    //交班值班领导
    changeShiftInfoCreateEntity.handoverLeader = duty.dutyLeader;
    changeShiftInfoCreateEntity.handoverLeaderId = duty.dutyLeaderId;
    //交班值班领导电话
    changeShiftInfoCreateEntity.handoverLeaderPhone = duty.dutyLeaderPhone;
    //排班的id
    changeShiftInfoCreateEntity.scheduleInfoId = duty.id;
    //重大事件id 调接口传入
    // changeShiftInfoCreateEntity.majorEventInfoId =xxx;

    majorEventInfoCreateEntity.dutyPerson = duty.dutyPerson??'';
    majorEventInfoCreateEntity.dutyPersonPhone = duty.dutyPersonPhone??'';
    majorEventInfoCreateEntity.dutyLeader = duty.dutyLeader??'';
    majorEventInfoCreateEntity.dutyLeaderPhone = duty.dutyLeaderPhone??'';
    majorEventInfoCreateEntity.dept = duty.companyName??'';

  }

  ///事件发生事件
  void showDatePickerEventTime() {
    CustomSelectFunction.selectDateTime(
        onCallBack: (String value) {
          changeShiftInfoCreateEntity.eventTime = value;
          majorEventInfoCreateEntity.eventTime = value;
          update();
        },
        helpText: '请选择事件发生时间');
  }

  ///上岗时间
  void selectOnDutyTime() {
    CustomSelectFunction.selectDateTime(
        onCallBack: (String value) {
          changeShiftInfoCreateEntity.onDutyTime = value;
          update();
        },
        helpText: '请选择上岗时间');
  }

  ///交班时间
  void selectHandoverTime() {
    CustomSelectFunction.selectDateTime(
        onCallBack: (String value) {
          changeShiftInfoCreateEntity.handoverTime = value;
          update();
        },
        helpText: '请选择交班时间');
  }

  ///接班时间
  void selectJBTime() {
    CustomSelectFunction.selectDateTime(
        onCallBack: (String value) {
          changeShiftInfoCreateEntity.takeoverTime = value;
          update();
        },
        helpText: '请选择接班时间');
  }

  ///创建交接班信息
  void confirm() async {
    //交班时间
    if (changeShiftInfoCreateEntity.handoverTime!.isEmpty) {
      AppToast.showWarning('请输入交班时间');
      return;
    }
    //接班人员
    if (changeShiftInfoCreateEntity.takeoverPerson!.isEmpty) {
      AppToast.showWarning('请输入接班人员');
      return;
    }
    //接班人员电话
    if (changeShiftInfoCreateEntity.takeoverPersonPhone!.isEmpty) {
      AppToast.showWarning('请输入接班人员电话');
      return;
    }
    //接班时间
    if (changeShiftInfoCreateEntity.takeoverTime!.isEmpty) {
      AppToast.showWarning('请输入接班时间');
      return;
    }
    //接班班次名称
    if (changeShiftInfoCreateEntity.takeoverScheduleName!.isEmpty) {
      AppToast.showWarning('请输入班次名称');
      return;
    }
    //值班领导
    if (changeShiftInfoCreateEntity.takeoverLeader!.isEmpty) {
      AppToast.showWarning('请输入值班领导');
      return;
    }
   //获取重大危险id
    final res = await HttpUtil()
        .generatePostRequest(
      Interface.createMajorChangeShiftInfo,
      data: majorEventInfoCreateEntity.toJson(),
    )
        .execute(isShowLoading: false, isShowToast: false);
    res.onSuccess((result) async {
      if (result.data != null) {
        //传入重大危险id
        if(result.data is int){
          changeShiftInfoCreateEntity.majorEventInfoId=result.data as int;
        }
        final res = await HttpUtil()
            .generatePostRequest(
          Interface.createChangeShiftInfo,
          data: changeShiftInfoCreateEntity.toJson(),
        )
            .execute(isShowLoading: false, isShowToast: false);
        res.onSuccess((result) async {
          if (result.data != null) {
            Get.back();
            AppToast.showSuccess('创建成功~');
            Get.find<DutyMgmtPageController>().getDayInfoList();
            Get.find<DutyMgmtPageController>().getMonthEvent();
          }
        });
        res.onError((e) {
          AppToast.showError(e.message);
        });
      }
    });
    res.onError((e) {
      AppToast.showError(e.message);
    });

  }
}
